package com.senseuni.da.receiver

import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.database.Cursor
import com.elvishew.xlog.XLog
import com.senseuni.da.utils.EventUtil

class MainBroadcast : BroadcastReceiver() {
    private var lastTime:Long = 0
    override fun onReceive(context: Context?, intent: Intent?) {
        val action = intent?.action
        if (Intent.ACTION_TIME_TICK == action) {
            if (System.currentTimeMillis() - lastTime < 30000) {
//                XLog.d("no need repeat send time event")
                return
            }
            lastTime = System.currentTimeMillis()
            EventUtil.sendTimeEvent(0)
        } else if (Intent.ACTION_TIMEZONE_CHANGED == action) {
            XLog.d("ACTION_TIMEZONE_CHANGED")
            EventUtil.sendTimeEvent(0)
        }

    }

    private fun getDownloadStatus(downloadManager: DownloadManager, downloadId: Long): Int {
        val query = DownloadManager.Query().setFilterById(downloadId)
        val c: Cursor? = downloadManager.query(query)
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    return c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS))
                }
            } finally {
                c.close()
            }
        }
        return -1
    }
}